﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Kugar.Core.Data;
using Kugar.Core.BaseStruct;
using Kugar.Core.ExtMethod;
using Kugar.Core.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //var type = typeof (int).GencentNullableType();

            //MutileKeyDictionary();

            //PoolTest();

            //ReflectionTest();
            TaskTest();
        }

        private static void TaskTest()
        {
            Console.WriteLine("mission start");

            var t = new CycTask();

            t.CycleType = CycleScheduleType.PerHour;
            t.ExecuteTimeSpan=new DateTime(1,1,1,0,28,0);

            TaskManager.Default.Add(t);

            Console.ReadLine();
        }

        public class CycTask:TaskItem_CycleSchedule
        {
            public int i = 0;
            protected override void RunTask(object state)
            {
                Console.WriteLine(i.ToString());
                i++;
                base.OnTaskProcessComplated();
            }
        }

        private static void ByteStreamTest()
        {
            var tempBytes = new byte[]
                                {
                                    1,2,3,4,5,6,7,8,9,10
                                };


        }

        private static void ReflectionTest()
        {
            var t = new Class1();

            t.FastSetValue("Property1.Property2","1111");

            var ret=t.FastGetValue("Property1.Property2");

            var ret2 = t.FastGetValue("Property1.Property2");
        }

        public class Class1
        {
            public Class1()
            {
                Property1=new Class2();
            }

            public Class2 Property1 { set; get; }
        }

        public class Class2
        {
            public Class2()
            {
                Property2 = "sdfsd";
            }

            public string Property2 { set; get; }
        }


        private static RecyclablePool<IKugarConnection> dbHelper = null;

        private static int _padding;

        static void PoolTest()
        {
            dbHelper = DatabaseHelperFactory.Create(DatabaseType.Sqlite,
                             @"data source=C:\datatest",
                             2, 1);

            for (int i = 1; i <= 5; i++)
            {
                Thread t = new Thread(Worker);

                t.Name = "thread" + i;

                // Start the thread, passing the number.
                //
                t.Start(i);
            }

            //var cnn1 = dbHelper.Push();
            //var cnn2 = dbHelper.Push();
            //var cnn3 = dbHelper.Push();
            //var cnn4 = dbHelper.Push();

            Thread.Sleep(10000);

            // The main thread starts out holding the entire
            // semaphore count. Calling Release(3) brings the 
            // semaphore count back to its maximum value, and
            // allows the waiting threads to enter the semaphore,
            // up to three at a time.
            //
            Console.WriteLine("Main thread calls Release(3).");


            //dbHelper.Dispose();

            Console.WriteLine("Main thread exits.");
        }

        private static void Worker(object num)
        {
            // Each worker thread begins by requesting the
            // semaphore.
            Console.WriteLine("Thread {0} begins " +
                "and waits for the semaphore.", num);

            var cnn=dbHelper.Push();

            // A padding interval to make the output more orderly.
            int padding = Interlocked.Add(ref _padding, 100);

            Console.WriteLine("Thread {0} enters the semaphore.", num);

            // The thread's "work" consists of sleeping for 
            // about a second. Each thread "works" a little 
            // longer, just to make the output more orderly.
            //
            Thread.Sleep(5000);

            cnn.Dispose();

            Console.WriteLine("Thread {0} releases the semaphore.", num);
            //Console.WriteLine("Thread {0} previous semaphore count: {1}",
            //    num, dbHelper.LockerCount);
        }

        static void MMFTest()
        {
            var t = new MemoryMappingFile(@"c:\ssss.sql");
            var buf1 = new byte[1000];
            var buf2 = new byte[1000];

            using (t)
            {
                using (var m=t.CreateMappingStream(40960,1000))
                {
                    m.Seek(10L, SeekOrigin.Begin);
                    m.Read(buf1, 0, 1000);
                }
            }

            using (var fs = new FileStream(@"c:\ssss.sql", FileMode.Open))
            {
                fs.Seek(40960, SeekOrigin.Begin);
                fs.Seek(10L, SeekOrigin.Current);
                fs.Read(buf2, 0, 1000);
            }

            for (int i = 0; i < 1000; i++)
            {
                if (buf1[i]!=buf2[i])
                {
                    
                }
            }
        }

        static void FastReflectionTest()
        {
            
        }

        static void MutileKeyDictionary()
        {
            var dic = new Dictionary<string, int>();

            dic.Add("0",0);

            var mdic = new MutileKeysDictionary<string, string, int>();

            mdic.Add("0", "0", 0);
            

            var t1 = new Stopwatch();
            var t2 = new Stopwatch();

            long time1 = 0;
            long time2 = 0;

            t1.Start();

            for (int i = 0; i < 10; i++)
            {
                dic.ContainsKey("0");
                //dic = new Dictionary<string, int>();
                //for (int j = 0; j < 10000; j++)
                //{
                //    dic.Add(j.ToString(),i);
                //}
            }

            t1.Stop();

            time1 = t1.ElapsedMilliseconds;


            t2.Start();

            for (int i = 0; i < 10; i++)
            {
                mdic.ContainsKey("0", "0");
                //mdic = new MutileKeysDictionary<string, string, int>();
                //for (int j = 0; j < 10000; j++)
                //{
                //    mdic.Add(j.ToString(),j.ToString(), j);
                //}
            }

            t2.Stop();

            time2 = t2.ElapsedMilliseconds;

            Console.WriteLine(time1);
            Console.WriteLine(time2);

            Console.Read();
        }

        //static void TestDataTable()
        //{
        //    long tblTime = 0;
        //    long ltblTIme = 0;

        //    for (int i = 0; i < 10; i++)
        //    {
        //        var watch2 = new Stopwatch();

        //        var ltbl = new LightDataTable();

        //        ltbl.Columns.Add(new LightDataColumn("A1", typeof(int)));
        //        ltbl.Columns.Add(new LightDataColumn("A2", typeof(DateTime)));
        //        ltbl.Columns.Add(new LightDataColumn("A3", typeof(string)));
        //        ltbl.Columns.Add(new LightDataColumn("A4", typeof(double)));
        //        ltbl.Columns.Add(new LightDataColumn("A5", typeof(short)));
        //        ltbl.Columns.Add(new LightDataColumn("A6", typeof(object)));
        //        watch2.Start();
        //        for (int j = 0; j < 100000; j++)
        //        {
        //            var row = ltbl.NewRow();

        //            row["A1"] = j;
        //            row["A2"] = DateTime.Now;
        //            row["A3"] = "ltb" + j;
        //            row["A4"] = (double)j;
        //            row["A5"] = (short)j;
        //            row["A6"] = new object();

        //            ltbl.Rows.Add(row);
        //        }

        //        watch2.Stop();

        //        ltblTIme += watch2.ElapsedMilliseconds;
        //    }


        //    for (int i = 0; i < 10; i++)
        //    {
        //        var watch1 = new Stopwatch();

        //        watch1.Start();

        //        var tbl = new DataTable();

        //        tbl.Columns.Add(new DataColumn("A1", typeof(int)));
        //        tbl.Columns.Add(new DataColumn("A2", typeof(DateTime)));
        //        tbl.Columns.Add(new DataColumn("A3", typeof(string)));
        //        tbl.Columns.Add(new DataColumn("A4", typeof(double)));
        //        tbl.Columns.Add(new DataColumn("A5", typeof(short)));
        //        tbl.Columns.Add(new DataColumn("A6", typeof(object)));

        //        for (int j = 0; j < 100000; j++)
        //        {
        //            var row = tbl.NewRow();

        //            row["A1"] = j;
        //            row["A2"] = DateTime.Now;
        //            row["A3"] = "tbl" + j;
        //            row["A4"] = (double)j;
        //            row["A5"] = (short)j;
        //            row["A6"] = new object();

        //            tbl.Rows.Add(row);
        //        }

        //        watch1.Stop();

        //        tblTime += watch1.ElapsedMilliseconds;
        //    }




        //    tblTime = tblTime / 10;
        //    ltblTIme = ltblTIme / 10;

        //    Console.Write(tblTime);
        //    Console.WriteLine();
        //    Console.Write(ltblTIme);

        //    Console.Read();
        //}
    }
}
